package com.bootdo.repertory.controller;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.*;
import com.bootdo.repertory.domain.PorderDO;
import com.bootdo.repertory.service.PorderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 进货订单
 * 
 * @author wang_wan_yu
 * @email 1992lcg@163.com
 * @date 2020-03-02 11:10:12
 */
 
@Controller
@RequestMapping("/repertory/porder")
public class PorderController {
	@Autowired
	private PorderService porderService;


	@Autowired
	private FileService sysFileService;

	@Autowired
	private BootdoConfig bootdoConfig;
	static String img;
	/*@Autowired
	private GoodsService goodsService;*/
	private String prefix = "repertory/porder";
	@GetMapping()
	@RequiresPermissions("repertory:porder:porder")
	String Porder(){
	    return "repertory/porder/porder";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("repertory:porder:porder")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PorderDO> porderList = porderService.list(query);
		int total = porderService.count(query);
		PageUtils pageUtils = new PageUtils(porderList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("repertory:porder:add")
	String add(){
	    return "repertory/porder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("repertory:porder:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		PorderDO porder = porderService.get(id);
		model.addAttribute("porder", porder);
		/*GoodsDO goods = goodsService.get(id);
		model.addAttribute("data", goods);*/
	    return "repertory/porder/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("repertory:porder:add")
	public R save(PorderDO porder){
		if(porderService.save(porder)>0){
			PorderDO porderDO = porderService.getInfo(porder);
			return R.ok(porderDO.getId()+"");
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("repertory:porder:edit")
	public R update(PorderDO porder/*,@RequestParam("ids") Integer id,GoodsDO goods*/){
		porderService.update(porder);
		return R.ok(porder.getId().toString());
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("repertory:porder:remove")
	public R remove(Long id){
		if(porderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("repertory:porder:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		porderService.batchRemove(ids);
		return R.ok();
	}


	//返回树状图页面
	@GetMapping("/addView")
	String insertView() {
		return  prefix + "/insertView";
	}


	@ResponseBody
	@PostMapping("/upload")
    R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName);
		FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
		img=fileName;
		System.out.println(fileName);
		try {
			FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath(), fileName);
		} catch (Exception e) {
			return R.error();
		}

		if (sysFileService.save(sysFile) > 0){
			return R.ok().put("fileName",sysFile.getUrl());
		}
		return R.error();
	}



	/**
	 * 图片修改
	 */
	@ResponseBody
	@RequestMapping("/updatePicture")
	public R updatePicture(@RequestParam(value = "file",required = false) MultipartFile[] files){

		FileDO fileDO = new FileDO();

		String filePath = bootdoConfig.getUploadPath();
		System.out.println("路径:"+filePath);
		for (MultipartFile file:files){
			String oldName = file.getOriginalFilename();
			System.out.println("旧名字是:"+oldName);
			String conversion = oldName.split("\\.")[oldName.split("\\.").length-1];
			String newName = new Date().getTime()+""+(int)(Math.random()*100000)+"."+conversion;
			System.out.println("新名字:"+newName);
			try {
				File file1 = new File(filePath);
				if (!file1.exists()){
					file1.mkdirs();
				}

				String PathName = "/files/"+newName;
				String newFilePath = filePath+newName;
				System.out.println("最后的路径:"+newFilePath);

				fileDO.setUrl(PathName);
				fileDO.setType(1);
				fileDO.setCreateDate(new Date());
				sysFileService.save(fileDO);
			//	picture.setPath(PathName);

				file.transferTo(new File(newFilePath));

				/*if (){
					R.ok();
				}*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok();
	}
}
